package com.jsy.common.util.m3u8.downloadAndPlayer;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;

/**
 * M3U8 播放器
 */
public class M3U8Player extends Application {
    private MediaPlayer mediaPlayer;
    private ProgressBar progressBar;
    private Button playButton;
    private Button pauseButton;
    private Button stopButton;
    
    private String mediaUrl;

    public M3U8Player(String mediaUrl) {
        this.mediaUrl = mediaUrl;
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            // 创建媒体对象
            Media media = new Media(new File(mediaUrl).toURI().toString());
            mediaPlayer = new MediaPlayer(media);
            
            // 创建媒体视图
            MediaView mediaView = new MediaView(mediaPlayer);
            mediaView.setFitWidth(800);
            mediaView.setFitHeight(600);
            
            // 创建控制按钮
            playButton = new Button("播放");
            playButton.setOnAction(e -> mediaPlayer.play());
            
            pauseButton = new Button("暂停");
            pauseButton.setOnAction(e -> mediaPlayer.pause());
            
            stopButton = new Button("停止");
            stopButton.setOnAction(e -> mediaPlayer.stop());
            
            // 创建进度条
            progressBar = new ProgressBar(0);
            progressBar.setPrefWidth(600);
            
            // 监听播放进度
            mediaPlayer.currentTimeProperty().addListener((observable, oldValue, newValue) -> {
                if (mediaPlayer.getTotalDuration() != Duration.UNKNOWN) {
                    progressBar.setProgress(newValue.toSeconds() / mediaPlayer.getTotalDuration().toSeconds());
                }
            });
            
            // 创建底部控制栏
            HBox controlBox = new HBox(10);
            controlBox.getChildren().addAll(playButton, pauseButton, stopButton, progressBar);
            
            // 创建主布局
            BorderPane root = new BorderPane();
            root.setCenter(mediaView);
            root.setBottom(controlBox);
            
            // 创建场景
            Scene scene = new Scene(root, 800, 650);
            primaryStage.setScene(scene);
            primaryStage.setTitle("M3U8 播放器");
            primaryStage.show();
            
            // 窗口关闭时释放资源
            primaryStage.setOnCloseRequest(e -> {
                if (mediaPlayer != null) {
                    mediaPlayer.stop();
                    mediaPlayer.dispose();
                }
            });
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.dispose();
        }
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("请提供M3U8文件路径或URL");
            return;
        }
        launch(args);
    }
}    